# ============================================================
# Thomas König & Stefan Eschenwecker
# The European Court of Justice and legal European integration
# 
# This script produces the regression model object 
# for the model incorporating additional case characteristics
# (see Table A4 in online appendix).
# ============================================================

# load packages
library(rstan)
library(brms)
library(tidyverse)

# set options for the script
options(mc.cores = parallel::detectCores())
options(scipen = 10)


# load data
CourtData <- read.csv("Court/Data/PreparedCourtData.csv")

# prepare data
# (DV + IVs as ordered factors)
# (remove cases where all actors claimed not applicable)
CourtData <- CourtData %>%
  mutate(
    pos_cjeu_ln = factor(pos_cjeu_ln,
      ordered = T,
      levels = c("PS", "Ambi", "ME")
    ),
    supra_signal = factor(supra_signal,
      ordered = F,
      levels = c( "Uninfo", "Cont", "Weak PS", "Strong PS", "Weak ME", "Strong ME")
    ),
    supra_signal_int = factor(supra_signal_int,
      ordered = T,
      levels = c("No Intensity", "Weak Intensity", "Strong Intensity"))
  ) %>%
  filter(all_not_applic == 0) %>% 
  mutate(about_derogation = ifelse(about_derogation == "yes", "yes", "no"),
         about_direct_effect = ifelse(about_direct_effect == "yes", "yes", "no"),
         about_validity = ifelse(about_validity == "yes", "yes", "no"))


# estimate regression model
# (automatically save the object)
# (Hint: takes some time to run)
OrdProbModel <- brm(
    bf(
    pos_cjeu_ln ~ supra_signal + net_ms_pref_std +
      share_judges_std + about_derogation +
      about_validity + about_direct_effect + 
      count_subject_keywords + 
      (1 | iuropa_case_id) + (1 | year_lodged)
  ) +
    lf(
      disc ~ 0 + mo(supra_signal_int) + ms_signal_int_std +
        share_judges_std + about_derogation +
        about_validity + about_direct_effect + 
        count_subject_keywords + 
        (1 | iuropa_case_id) + (1 | year_lodged),
       cmc = F), 
  family = cumulative(link = "probit"),
  prior = c(
    # default alpha = 1 Dirichlet prior for monotonic effect
    prior(normal(0, 1), class = b), # standardized location predictors
    prior(exponential(1 / 0.463), class = sd, group = iuropa_case_id), # 95% interval of 0.25 to 4
    prior(normal(-0.43, 2), class = Intercept, coef = 1), # location is qnorm of 1/3
    prior(normal(0.43, 2), class = Intercept, coef = 2), # location is qnorm of 2/3
    prior(normal(0, log(4) / 2), class = b, dpar = disc), # expect difference by factor of max 4
    prior(exponential(1 / 0.463), class = sd, dpar = disc) # 95% interval of 0.25 to 4
  ),
  init = 0, # avoid rejected starting values
  data = CourtData,
  chains = 4, cores = 4, warmup = 1500, iter = 3500,
  seed = 1408, control = list(adapt_delta = 0.99),
  file = "Court/Models/UnequalPrecision_CaseCovariates",
  backend = "cmdstanr"
)

# print summary
summary(OrdProbModel)

# check for non-convergence
shinystan::launch_shinystan(OrdProbModel)
